package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.entity.Address;
import com.project.myPackage.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.baomidou.mybatisplus.extension.api.R.*;

@Api(tags = "地址管理")
@Slf4j
@RestController
@RequestMapping("/")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据用户id获取所有地址信息")
    @RequestMapping("/getAllAddressByUserId")
    public R<?> getAllAddressByUserId(Integer user_id) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUser_id, user_id);
        List<Address> list = addressService.list(queryWrapper);
        return  R.success(list,"获取所有地址信息列表成功！");
    }

    @ApiOperation(value = "新增地址信息")
    @PostMapping("/addAddress")
    public R<?> addAddress(@RequestBody Address address) {
        //判断是否是默认地址 默认地址为1，非默认地址为0
        if (address.getIs_default().equals(1)) {
            LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Address::getUser_id, address.getUser_id());
            List<Address> list = addressService.list(queryWrapper);
            for (Address address1 : list) {
                //所有该用户地址设置为非默认地址
                address1.setIs_default(0);
                addressService.updateById(address1);
            }
        }
        //更新默认地址
        boolean save = addressService.save(address);
        if (save) {
            return R.success(null,"新增地址信息成功！");
        }
        return R.error("新增地址信息失败！");
    }

    @ApiOperation(value = "删除地址信息")
    @RequestMapping("/deleteAddress")
    public R<?> deleteAddress(Integer address_id) {
        boolean remove = addressService.removeById(address_id);
        if (remove) {
            return R.success(null,"删除地址信息成功！");
        }
        return R.error("删除地址信息失败！");
    }

    @ApiOperation(value = "修改地址信息")
    @PostMapping("/updateAddress")
    public R<?> updateAddress(@RequestBody  Address address) {
        if(address.getAddress_id() == null) return R.error("地址id不能为空！");
        if(address.getUser_id()== null) return R.error("用户id不能为空！");
        //判断是否是默认地址 默认地址为1，非默认地址为0
        if (address.getIs_default().equals(1)) {
            LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Address::getUser_id, address.getUser_id());
            List<Address> list = addressService.list(queryWrapper);
            for (Address address1 : list) {
                //所有该用户地址设置为非默认地址
                address1.setIs_default(0);
                addressService.updateById(address1);
            }
        }
        //更新默认地址
        boolean update = addressService.updateById(address);
        if (update) {
            return R.success(null,"修改地址信息成功！");
        }
        return R.error("修改地址信息失败！");
    }
}
